<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 *
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 *
 * @package      FeatherCMS
 * @subpackage   Libs
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://julien.portalier.com
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

class FileUpload
{
	function save($name, $path_to, $override=false)
	{
		// checks uploaded file
		$ok = $this->check($name);
		
		// error?
		if ($ok !== true)
			return $ok;
	
		// moves uploaded file
		if (is_dir($path_to))
			$path_to .= DS.str_replace(array("\\", "'", '"', " "), array('', '-', '-', '_'), $_FILES[$name]['name']);
		return move_uploaded_file($_FILES[$name]['tmp_name'], $path_to);
	}
	
	function saveMany($name, $path_to, $override=false)
	{
		$index = 0;
		$ret = true;
		
		// for each uploaded file
		do
		{
			// check
			$ok = $this->check($name, $index);
			
			if ($ok !== true)
				return $ok;
			
			// where to save
			$save_to = $path_to.DS.str_replace(array("\\", "'", '"', " "), array('', '-', '-', '_'), $_FILES[$name]['name'][$index]);
			
			if (!file_exists($save_to))
				$ret &= move_uploaded_file($_FILES[$name]['tmp_name'][$index], $save_to);
			else
				return s_("Error: '%s' already exists.", $_FILES[$name]['name'][$index]);
			
			$index++;
		}
		while(true);
		
		return $ret;
	}
	
	function check($name, $index=null)
	{
		if (!empty($_FILES[$name]))
		{
			if (is_null($index))
			{
				// single file
				if ($_FILES[$name]['error'] != UPLOAD_ERR_OK)
					return $this->getError($_FILES[$name]['error']);
				elseif (!is_uploaded_file($_FILES[$name]['tmp_name']))
					return _("Security alert: this file hasn't been uploaded!");
			}
			else
			{
				// one or more files
				if (empty($_FILES[$name]['name'][$index]))
					return false;
				elseif (!is_uploaded_file($_FILES[$name]['tmp_name'][$index]))
					return _("Security alert: this file hasn't been uploaded!");
				elseif ($_FILES[$name]['error'][$index] != UPLOAD_ERR_OK)
					return $this->getError($_FILES[$name]['error'][$index]);
			}
			return true;
		}
		return false;
	}
	
	function isAnimated($name)
	{
		if ($this->getType($name) == 'image/gif')
		{
			$filecontents = file_get_contents($this->getTmpFilename($name));
			$str_loc = 0;
			$count   = 0;
			
			// there is no point in continuing after we find a 2nd frame
			while ($count < 2)
			{
				$where1 = strpos($filecontents, "\x00\x21\xF9\x04", $str_loc);
				
				if ($where1 === false)
					break;
				else
				{
					$str_loc = $where1 + 1;
					$where2  = strpos($filecontents, "\x00\x2C", $str_loc);
					
					if ($where2 === false)
						break;
					else
					{
						if ($where1 + 8 == $where2)
							$count++;
						$str_loc = $where2 + 1;
					}
				}
			}
			return ($count > 1);
		}
		return false;
	}
	
	function getType($name)
	{
		return isset($_FILES[$name]['type']) ? $_FILES[$name]['type'] : null;
	}
	
	function getFilename($name)
	{
		return isset($_FILES[$name]['name']) ? $_FILES[$name]['name'] : null;
	}
	
	function getTmpFilename($name)
	{
		return isset($_FILES[$name]['tmp_name']) ? $_FILES[$name]['tmp_name'] : null;
	}
	
	function getError($code)
	{
		switch($code)
		{
			case UPLOAD_ERR_INI_SIZE:
				return s_("The uploaded file is too big (maximum is %s).", ini_get('upload_max_filesize'));
			
			case UPLOAD_ERR_FORM_SIZE:
				return s_("The uploaded file is too big (maximum is %s).", $_REQUEST['MAX_FILE_SIZE']);
			
			case UPLOAD_ERR_PARTIAL:
				return _("The file was only partially uploaded.");
			
			case UPLOAD_ERR_NO_FILE:
				#return _("No file was uploaded.");
				return false;
			
			case UPLOAD_ERR_NO_TMP_DIR:
				return _("Missing a temporary folder.");
			
			default:
				return _("An unknown file upload error occured.");	
		}
	}	
}
?>